home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / NT / CODE / CHAP05 / FONTVIEW / FONTVIEW.CPP next >
C/C++ Source or Header  |  1996-04-05  |  6KB  |  182 lines

  1. //***********************************************************************
  2. //
  3. //  FontView.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "FontView.h"
  9.  
  10. #define IDC_PRINT       100
  11. #define IDC_CHECKBOX    101
  12. #define IDC_LISTBOX     102
  13. #define IDC_SAMPLE      103
  14.  
  15. CMyApp myApp;
  16.  
  17. /////////////////////////////////////////////////////////////////////////
  18. // CMyApp member functions
  19.  
  20. BOOL CMyApp::InitInstance ()
  21. {
  22.     m_pMainWnd = new CMainWindow;
  23.     m_pMainWnd->ShowWindow (m_nCmdShow);
  24.     m_pMainWnd->UpdateWindow ();
  25.     return TRUE;
  26. }
  27.  
  28. /////////////////////////////////////////////////////////////////////////
  29. // CMainWindow message map and member functions
  30.  
  31. BEGIN_MESSAGE_MAP (CMainWindow, CWnd)
  32.     ON_WM_CREATE ()
  33.     ON_BN_CLICKED (IDC_PRINT, OnPushButtonClicked)
  34.     ON_BN_CLICKED (IDC_CHECKBOX, OnCheckBoxClicked)
  35.     ON_LBN_SELCHANGE (IDC_LISTBOX, OnSelChange)
  36. END_MESSAGE_MAP ()
  37.  
  38. CMainWindow::CMainWindow ()
  39. {
  40.     CString strWndClass = AfxRegisterWndClass (
  41.         0,
  42.         myApp.LoadStandardCursor (IDC_ARROW),
  43.         (HBRUSH) (COLOR_3DFACE + 1),
  44.         myApp.LoadStandardIcon (IDI_APPLICATION)
  45.     );
  46.  
  47.     CreateEx (0, strWndClass, "FontView",
  48.         WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
  49.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  50.         NULL, NULL, NULL);
  51.  
  52.     CRect rect (0, 0, m_cxChar * 68, m_cyChar * 26);
  53.     CalcWindowRect (&rect);
  54.  
  55.     SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (),
  56.         SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
  57. }
  58.  
  59. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  60. {
  61.     if (CWnd::OnCreate (lpcs) == -1)
  62.         return -1;
  63.  
  64.     CClientDC dc (this);
  65.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
  66.  
  67.     m_fontMain.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
  68.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  69.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
  70.  
  71.     CFont* pOldFont = dc.SelectObject (&m_fontMain);
  72.     TEXTMETRIC tm;
  73.     dc.GetTextMetrics (&tm);
  74.     m_cxChar = tm.tmAveCharWidth;
  75.     m_cyChar = tm.tmHeight + tm.tmExternalLeading;
  76.     dc.SelectObject (pOldFont);
  77.  
  78.     CRect rect (m_cxChar * 2, m_cyChar, m_cxChar * 48, m_cyChar * 2);
  79.     m_ctlLBTitle.Create ("Typefaces", WS_CHILD | WS_VISIBLE | SS_LEFT,
  80.         rect, this);
  81.  
  82.     rect.SetRect (m_cxChar * 2, m_cyChar * 2, m_cxChar * 48,
  83.         m_cyChar * 18);
  84.     m_ctlListBox.CreateEx (WS_EX_CLIENTEDGE, "listbox", NULL,
  85.         WS_CHILD | WS_VISIBLE | LBS_STANDARD, rect.left, rect.top,
  86.         rect.Width (), rect.Height (), m_hWnd, (HMENU) IDC_LISTBOX,
  87.         NULL);
  88.  
  89.     rect.SetRect (m_cxChar * 2, m_cyChar * 19, m_cxChar * 48,
  90.         m_cyChar * 20);
  91.     m_ctlCheckBox.Create ("Show TrueType fonts only",  WS_CHILD |
  92.         WS_VISIBLE | BS_AUTOCHECKBOX, rect, this, IDC_CHECKBOX);
  93.  
  94.     rect.SetRect (m_cxChar * 2, m_cyChar * 21, m_cxChar * 66,
  95.         m_cyChar * 25);
  96.     m_ctlGroupBox.Create ("Sample",  WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
  97.         rect, this, (UINT) -1);
  98.  
  99.     rect.SetRect (m_cxChar * 4, m_cyChar * 22, m_cxChar * 64,
  100.         (m_cyChar * 99) / 4);
  101.     m_ctlSampleText.Create ("", WS_CHILD | WS_VISIBLE | SS_CENTER, rect,
  102.         this, IDC_SAMPLE);
  103.  
  104.     rect.SetRect (m_cxChar * 50, m_cyChar * 2, m_cxChar * 66,
  105.         m_cyChar * 4);
  106.     m_ctlPushButton.Create ("Print Sample", WS_CHILD | WS_VISIBLE |
  107.         WS_DISABLED | BS_PUSHBUTTON, rect, this, IDC_PRINT);
  108.  
  109.     m_ctlLBTitle.SetFont (&m_fontMain, FALSE);
  110.     m_ctlListBox.SetFont (&m_fontMain, FALSE);
  111.     m_ctlCheckBox.SetFont (&m_fontMain, FALSE);
  112.     m_ctlGroupBox.SetFont (&m_fontMain, FALSE);
  113.     m_ctlPushButton.SetFont (&m_fontMain, FALSE);
  114.  
  115.     FillListBox ();
  116.     return 0;
  117. }
  118.  
  119. void CMainWindow::PostNcDestroy ()
  120. {
  121.     delete this;
  122. }
  123.  
  124. void CMainWindow::OnPushButtonClicked ()
  125. {
  126.     MessageBox ("This feature is currently unimplemented. Sorry!",
  127.         "Error", MB_ICONINFORMATION | MB_OK);
  128. }
  129.  
  130. void CMainWindow::OnCheckBoxClicked ()
  131. {
  132.     FillListBox ();
  133.     OnSelChange ();
  134. }
  135.  
  136. void CMainWindow::OnSelChange ()
  137. {
  138.     int nIndex = m_ctlListBox.GetCurSel ();
  139.  
  140.     if (nIndex == LB_ERR) {
  141.         m_ctlPushButton.EnableWindow (FALSE);
  142.         m_ctlSampleText.SetWindowText ("");
  143.     }
  144.     else {
  145.         m_ctlPushButton.EnableWindow (TRUE);
  146.         if ((HFONT) m_fontSample != NULL)
  147.             m_fontSample.DeleteObject ();
  148.  
  149.         CString strFaceName;
  150.         m_ctlListBox.GetText (nIndex, strFaceName);
  151.  
  152.         m_fontSample.CreateFont (-m_cyChar * 2, 0, 0, 0, FW_NORMAL,
  153.             0, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
  154.             CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
  155.             FF_DONTCARE, strFaceName);
  156.  
  157.         m_ctlSampleText.SetFont (&m_fontSample);
  158.         m_ctlSampleText.SetWindowText ("AaBbCcDdEeFfGg");
  159.     }
  160. }
  161.  
  162. void CMainWindow::FillListBox ()
  163. {
  164.     m_ctlListBox.ResetContent ();
  165.  
  166.     CClientDC dc (this);
  167.     ::EnumFontFamilies ((HDC) dc, NULL, (FONTENUMPROC) EnumFontFamProc,
  168.         (LPARAM) this);
  169. }
  170.  
  171. int CALLBACK CMainWindow::EnumFontFamProc (ENUMLOGFONT* lpelf,
  172.     NEWTEXTMETRIC* lpntm, int nFontType, LPARAM lParam)
  173. {
  174.     CMainWindow* pWnd = (CMainWindow*) lParam;
  175.  
  176.     if ((pWnd->m_ctlCheckBox.GetCheck () == BST_UNCHECKED) ||
  177.         (nFontType & TRUETYPE_FONTTYPE))
  178.         pWnd->m_ctlListBox.AddString (lpelf->elfLogFont.lfFaceName);
  179.  
  180.     return 1;
  181. }
  182.